package 设计模式.j_享元模式;

import java.util.EnumMap;
import java.util.Map;

/**
 * @author caihe
 * @date 2023/3/13 14:14
 */
public class PotionFactory {

    private final Map<PotionType, Potion> potionMap;

    public PotionFactory() {
        potionMap = new EnumMap<>(PotionType.class);
    }

    public Potion createPotion(PotionType potionType) {
        Potion potion = potionMap.get(potionType);
        if (potion == null) {
            switch (potionType) {
                case RED_HEALING:
                    potion = new HealingPotion("红色");
                    break; 
                case BLACK_HEALING:
                    potion = new HealingPotion("黑色");
                    break;
                case INVISIBILITY:
                    potion = new InvisibilityPotion();
                    break;
                default:
                    break;
            }
            potionMap.put(potionType, potion);
        }
        return potion;
    }


}
